#!/bin/bash
# nakedeb trixie build script
# nak3dev @ 2020-2025 © WTFPL <https://arpinux.org>

# cfg ------------------------------------------------------------------
PROJECT="nakedeb project"
HOMEPAGE="https://nakedeb.arpinux.org"
CONTACT="nakedeb@arpinux.org"
VERSION="1.6-$(date +%Y%m)-amd64"
DISTRO="trixie"
LOG="nakedeb.log"

# stop on error and try to trace it ------------------------------------
set -eE # same as: `set -o errexit -o errtrace`

# root ? ---------------------------------------------------------------
if [ $(whoami) != root ]; then
    echo -e "\nERROR: nakedbuild needs root\n"; exit 1
fi

# dep ? ----------------------------------------------------------------
if ! dpkg-query -W live-build &>"/dev/null"; then
    echo -e "\nERROR: nakedbuild needs live-build\n"; exit 1
fi

# args & help ----------------------------------------------------------
if [ "$1" == "iso" ]; then NKD="nakedeb-${VERSION}"
elif [ "$1" == "clean" ]; then lb clean && rm -Rf cache && exit 0
else
    # no args or wrong args > help
    echo -e "\n wrong arg... need help ?\n"; cat HOWTO.md; exit 1
fi

# open log -------------------------------------------------------------
# opening log file with host system informations
echo -e "----\n${PROJECT} - ${HOMEPAGE} - ${CONTACT}" > ${LOG}
echo -e "$(date)" >> ${LOG}
echo -e "system: $(uname -a)" >> ${LOG}
echo -e "tools: live-build-$(lb -v) - Debian-$(cat /etc/debian_version)" >> ${LOG}
echo -e "> building ${NKD}" >> ${LOG}

# setup preseed --------------------------------------------------------
# put preseed file in build dir for the installation end config
PRD="config/includes.chroot/usr/bin/nkd"
echo -e "----\nINFO: setting up nakedeb preseed file" >> ${LOG}
mkdir -p ${PRD}
install -m 755 ressources/nakedpreseed ${PRD}/nakedpreseed

# setup apt ------------------------------------------------------------
# setting up nakedeb repository gpg key
KRD="config/includes.chroot/usr/share/keyrings"
echo -e "----\nINFO: setting up nakedeb repository gpg key" >> ${LOG}
mkdir -p ${KRD}
install -m 644 ressources/gpg/nakedeb.gpg ${KRD}/nakedeb-keyring.gpg

# configure build ------------------------------------------------------
echo -e "----\nINFO: ${NKD} configuration" >> ${LOG}
# common config
lb config --iso-volume "${NKD}" \
    --iso-application "${NKD}" \
    --iso-publisher "${PROJECT}; ${HOMEPAGE}; ${CONTACT}" \
    --parent-distribution "${DISTRO}" \
    --debian-installer-distribution "${DISTRO}" \
    --distribution "${DISTRO}" | tee -a ${LOG}

# apt-cacher-ng config
if dpkg-query -W apt-cacher-ng &>"/dev/null"; then
    lb config --apt-http-proxy "http://127.0.0.1:3142" >> ${LOG}
fi

# build ISO ------------------------------------------------------------
echo -e "----\nINFO: building ${NKD}\n" >> ${LOG}
lb build

# rename & files -------------------------------------------------------
if test -f live-image-amd64.hybrid.iso; then
    echo -e "----\nINFO: renaming"
    ISODIR="nakedeb-${VERSION}"
    mkdir -p ${ISODIR}
    mv live-image-amd64.hybrid.iso ${ISODIR}/${NKD}.iso
    mv chroot.packages.install ${ISODIR}/${NKD}.pkgs
    mv ${LOG} ${ISODIR}/${NKD}.log
    cd ${ISODIR}
    echo -e "----\nINFO: calculate sha256sum"
    sha256sum ${NKD}.iso > ${NKD}.sha256
    echo -e "----\nINFO: make torrent"
    # trackers list https://github.com/ngosang/trackerslist
    transmission-create \
        -t udp://tracker.opentrackr.org:1337/announce \
        -t udp://tracker.torrent.eu.org:451/announce \
        -t udp://exodus.desync.com:6969/announce \
        -t udp://open.free-tracker.ga:6969/announce \
        -t udp://concen.org:6969/announce \
        ${NKD}.iso
    cd ../
    echo -e "----\nINFO: permissions"
    find ${ISODIR} -name "*" -type f -exec chmod 644 {} \;
    chown -R ${SUDO_USER}:${SUDO_USER} ${ISODIR}
    echo -e "----\nINFO: cleaning"
    lb clean
    echo -e "----\nbuild made in $(($SECONDS/60)) minutes\n"
    echo -e "MEMO: you should gpgsign sha256 files\n"; exit 0
else
    chmod 666 ${LOG}
    echo -e "----\nERROR: ISO not builded ... check your ${LOG} file !\n"; exit 1
fi
# eof ------------------------------------------------------------------
